home

Debugging C++ Programs with GDB.md


If you don’t use GDB but program in C/C++, you are missing out on a powerful debugging tool. Here’s a quick 5-minute tutorial to get you started.

Step 1: Compile with Debug Symbols

Compile your program using the -ggdb flag to include debug symbols for GDB.

# For C++
g++ main.cpp -o main -ggdb

# For C
gcc main.c -o main -ggdb

Step 2: Run GDB

Now, run GDB from the shell, pointing it to your compiled executable.

gdb ./main

(Note: If you aren’t using Linux, at least consider using WSL (Windows Subsystem for Linux) for a better debugging experience.)

You will see the (gdb) prompt.

Step 3: Basic GDB Commands

Step 4: Visualize Code with layout split

One helpful feature is the split layout, which displays both the source code and GDB commands simultaneously.

layout split

or to skip the assembly view

layout src

Step 5: Inspecting Variables

Use the print and display commands in GDB to inspect variables and expressions during debugging.

For example:

print variable_name
display expression

These commands help you check if anything is unexpected in your program.

Now go practice!!!